<?php
namespace Home\Controller;
use Think\Controller;
vendor('taobao.TopSdk');//引入淘宝SDK
class RefundController extends Controller {
	
	public function __construct() {
		parent::__construct();
		//var_dump($_SESSION['user']['username']);die;
		
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
	
    public function index(){
		$where['visitor_id'] = $_SESSION['user']['id'];
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shoptitle=empty($_REQUEST['shoptitle'])?'':trim($_REQUEST['shoptitle']);
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$title=empty($_REQUEST['title'])?'':trim($_REQUEST['title']);
		$orderno=empty($_REQUEST['orderno'])?'':trim($_REQUEST['orderno']);
		$phone=empty($_REQUEST['phone'])?'':trim($_REQUEST['phone']);
		$name=empty($_REQUEST['name'])?'t.pay_time':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		$shop_list=M('shop')->where($where)->field("id,shop_name")->select();
		
		$orders=D('orders');
		$res=$orders->order_list($pageNo,$pageSize,$starttime,$endtime,$orderno,$shoptitle,'',$phone,'refund',$name,$asc,'','','','','','');

		$this->assign('order_list',$res['order_list']);
		$this->assign('count',$res['count']);
		$this->assign('data',$res['data']);
		$this->assign('shop_list',$shop_list);
		$this->display();
    }
	
	public function refund_info(){
		$where['visitor_id'] = $_SESSION['user']['id'];
		$oid=empty($_REQUEST['oid'])?0:$_REQUEST['oid'];
		
		$shop=M('shop');
		$shop_list=$shop->field('id,shop_name')->where($where)->select(); 
		
		$order=D('orders');
		$orderinfo=$order->get_order_info($oid);
		
		$customer =D('Customer');
		$service_list=$customer->dropdown_list('service'); 
		
		$this->assign("orderinfo",$orderinfo); 
		$this->assign("shop_list",$shop_list);
		$this->assign("service_list",$service_list);
		$this->assign("jumpurl",$_SERVER['HTTP_REFERER']);
		$this->display();
    }
	
	public function refund_apply()
	{   
		$jumpurl=empty($_POST['jumpurl'])?'':$_POST['jumpurl'];
		$whereo['oid']=empty($_POST['oid'])?'0':$_POST['oid'];
		$outer_iid=empty($_POST['outer_iid'])?'':$_POST['outer_iid'];
		
		$data['refund_num']=empty($_POST['refund_num'])?'':$_POST['refund_num'];
		$data['refund_fee']=empty($_POST['refund_fee'])?'':$_POST['refund_fee'];
		$data['refund_payment']=empty($_POST['refund_payment'])?'':$_POST['refund_payment'];
		$data['refund_remarks']=empty($_POST['refund_remarks'])?'':$_POST['refund_remarks'];
		$data['visitor_id']	 = $_SESSION['user']['id'];
		
		if(empty($data['refund_num']) && empty($data['refund_fee']) && empty($data['refund_payment']))
		{   
		    $data['refund_operator']='';
			$data['refund_time']='';
		}
		else
		{   
		    $data['refund_operator']=empty($_POST['refund_operator'])?'':$_POST['refund_operator'];
			$data['refund_time']=date('Y-m-d H:i:s');
		}
		
		
		$outer_iid=empty($_POST['outer_iid'])?'':$_POST['outer_iid'];
		$goodstitle=mb_substr($outer_iid,0,-2,'utf-8');
		
		$trades=M('trades');
		$orders=M('orders');
		$whereo['visitor_id'] = $_SESSION['user']['id'];
		
		$tid=$orders->field('tid')->where($whereo)->find();
		$wheret['tid']=$tid['tid'];
		$wheret['visitor_id'] = $_SESSION['user']['id'];
		
		$orderinfo=$trades->where($wheret)->find();
		$rest=$trades->where($wheret)->save($data);
		
		$goods=M('goods');
		$whereg['title']=$goodstitle;
		$whereg['visitor_id'] = $_SESSION['user']['id'];
		
		$goodsinfo=$goods->where($whereg)->find();
		
		if($rest!==false)
		{
			if(empty($orderinfo['refund_num']))
			{
				$datag['stock']=$goodsinfo['stock']+$data['refund_num'];
			}
			else
			{
				$datag['stock']=$goodsinfo['stock']-$orderinfo['refund_num']+$data['refund_num'];
			}
			$datag['visitor_id']	 = $_SESSION['user']['id'];
			$resg=$goods->where($whereg)->save($datag);
			
		}
		
		$this->redirect('refund_list');
		
	}
	
	public function refund_list()
	{
		$where['visitor_id'] = $_SESSION['user']['id'];
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shoptitle=empty($_REQUEST['shoptitle'])?'':trim($_REQUEST['shoptitle']);
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$title=empty($_REQUEST['title'])?'':trim($_REQUEST['title']);
		$orderno=empty($_REQUEST['orderno'])?'':trim($_REQUEST['orderno']);
		$phone=empty($_REQUEST['phone'])?'':trim($_REQUEST['phone']);
		$name=empty($_REQUEST['name'])?'t.pay_time':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		/*$shop_list=M('shop')->field("id,shop_name")->where($where)->select();*/
	    
		$orders=D('orders');
		$res=$orders->order_list($pageNo,$pageSize,$starttime,$endtime,$orderno,'','',$phone,'refund_success',$name,$asc,'','','','','','');
		
		$this->assign('order_list',$res['order_list']);
		$this->assign('count',$res['count']);
		$this->assign('data',$res['data']);
		$this->assign('shop_list',$shop_list);
		$this->display();
		
	}
	
	
	public function download_info()
	{   
		$where['visitor_id'] = $_SESSION['user']['id'];
	
		$shoptitle=empty($_REQUEST['shoptitle'])?'':$_REQUEST['shoptitle'];
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		
		$data['shoptitle']=$shoptitle;
		$data['starttime']=$starttime;
		$data['endtime']=$endtime;
		
		$shop_list=M('shop')->field("id,shop_name")->where($where)->order('id desc')->select();
		
		$this->assign('shop_list',$shop_list); 
		$this->assign('data',$data);
		
		$this->display();
	}
	
	public function refund_download(){
		$visitor_id = $_SESSION['user']['id']; 
		/*if(isset($_POST['shoptitle']))
		{
			session('download',true);
			session('shoptitle',$_POST['shoptitle']);
			
		}
		
	    if($_SESSION['download'])
		{
			$where='1=1'; 
			$where.=" and visitor_id = '$visitor_id'";
			if(!empty($_SESSION['shoptitle']))
			{
				$where.=" and shop_name='".$_SESSION['shoptitle']."' ";
			}
			
			*/
			session('starttime',trim($_POST['starttime']));
			session('endtime',trim($_POST['endtime']));
			$shop=M('shouquan');
			$i=empty($_SESSION['i'])?0:$_SESSION['i'];
			$info=$shop->field('top_session,visitor_nick,visitor_id,group_id')->where("visitor_id = '$visitor_id'")->order('id')->select();
			if($info[0]['group_id']){
				$shopinfo = $shop->field('top_session,visitor_nick,visitor_id')->where("group_id = '".$info[0]['group_id']."'")->order('id')->select();
			}else{
				$shopinfo = $info;
			}
			foreach($shopinfo as $k=>$val){
				$shopinfo[$k]['top_session'] = base64_decode($val['top_session']);
			}
			
			
			$page=empty($_SESSION['page'])?1:$_SESSION['page'];
		    $pagesize=20;
			if($shopinfo[$i])
			{
				$res=$this->get_refunds($shopinfo[$i]['top_session'],$page,$pagesize,$_SESSION['starttime'],$_SESSION['endtime'],$shopinfo[$i]['visitor_id']);
				
				if($res['error'])
				{
					session('i',$i+1);
					session('page',1);	
				}
				else
				{
					if($res['page_count']<=$res['page'])
					{
						session('i',$i+1);
						session('page',1);
					}
					else
					{
						session('page',$page+1);	
					}
				}
				
				echo "<meta http-equiv='refresh' content='1'>";
				
			}
			else
			{   
				echo "<p>下载完成</p>";
				session('i',0);
				session('page',1);
				unset($_SESSION['download']);
				unset($_SESSION['starttime']);
				unset($_SESSION['endtime']);
				unset($_SESSION['shoptitle']);
				header('Location:download_info');
			}
			
		//}
		
	}
	
	
   public function get_refunds($sessionkey,$page,$pagesize,$starttime,$endtime,$visitor_id)
   {    
   		$appkey = '23416550';
		$secretkey = 'ebbc68fc906b6436f76b4f9cda829976';
	    $c = new \TopClient;
		$c->appkey = $appkey;
		$c->secretKey = $secretkey;
		$req = new \RefundsReceiveGetRequest;
		$req->setFields("refund_id, tid, oid, total_fee, buyer_nick, seller_nick, created, modified, order_status, status, good_status, has_good_return, refund_fee, payment, reason, desc, title, num, company_name, sid, refund_phase,refund_version, sku, attribute, outer_id, operation_contraint");
		//$req->setStatus("WAIT_SELLER_AGREE");
		//$req->setBuyerNick("hz0799");
		//$req->setType("fixed");
		$start_Date = empty($starttime)?date("Y-m-d H:i:s",time()-8640000):$starttime;
		$end_Date = empty($endtime) ? date("Y-m-d H:i:s",time()) : $endtime;
		$req->setStartModified($start_Date);
		
		$req->setEndModified($end_Date);
		
		
		
		$req->setPageNo($page);
		$req->setPageSize($pagesize);
		//$req->setUseHasNext('true');
		$resp = $c->execute($req, $sessionkey);  
		//var_dump($resp);exit;
        
		
		$count=(string)$resp->total_results;
		$page_count=ceil($count/$pagesize);
		
		$orders=M('orders');
		
	
		if(isset($resp->refunds))
		{
			
			if(!empty($starttime) || !empty($endtime))
			{
				echo "<p>时间：{$starttime} - {$endtime}</p>"; 
			}
			
			echo "<p>共：{$count} 条数据　　{$page_count} 页</p>"; 
			echo "<p>当前第 {$page} 页</p>";
			
			
			foreach($resp->refunds->refund as $k=>$v)
			{	 
			    $tid=(string)$v->tid;
				
				echo "<p>订单号：{$tid} </p>"; 
				
				
				$refund['refund_status']   = (string)$v->status;
				$refund['refund_id']       = (string)$v->refund_id;
				$refund['refund_fee']      = (string)$v->refund_fee;
				$refund['refund_created']  = (string)$v->created;
				$refund['refund_reason']   = (string)$v->reason;
				$refund['refund_desc']     = (string)$v->desc;
				$refund['refund_phase']    = (string)$v->refund_phase;
				$refund['refund_version']  = (string)$v->refund_version;
				$refund['good_status']     = (string)$v->good_status;
				$refund['has_good_return'] = (string)$v->has_good_return;
			    $refund['visitor_id']	   = $visitor_id;
				$oid=(string)$v->oid;
				$join="trades as t on t.tid=o.tid";
				$field="t.title,t.receiver_name,t.receiver_mobile,t.receiver_phone,t.receiver_state,t.receiver_city,t.receiver_district,t.receiver_town,t.receiver_address,".
			           "o.outer_iid,o.payment,o.id,o.oid,o.tid,o.num";
				$orderinfo=$orders->alias("as o")->join($join)->field($field)->where("oid='$oid' ")->find();
				
				if($orderinfo)
				{
					$reso=$orders->where("oid='".$oid."'")->save($refund);
					
					/* 退款成功且有退货的增加库存、库存记录 */
					if($refund['refund_status']=='SUCCESS' && $refund['has_good_return']=='true')
					{   
					    $shopinfo=M('shop')->field('id,shop_name')->where("visitor_id = '$visitor_id' and shop_name='".$orderinfo['title']."'")->find();
						
						$goodsinfo=M('goods')->field("id,code,stock")->where("visitor_id = '$visitor_id' and code='".$orderinfo['outer_iid']."'")->find(); 
						
						if($goodsinfo)
						{
							$goods_stock['stock']=$goodsinfo['stock']+$orderinfo['num'] ;
							$where_in['id'] = $goodsinfo['id'] ;
							$where_in['visitor_id'] = $visitor_id;
							$reg=M('goods')->where($where_in)->save($goods_stock); /* 退款加库存  */
							
							if($reg)
							{
								$log['shop']=$shopinfo['id'] ;
								$log['name']=$orderinfo['receiver_name'] ;
								$log['phone']=empty($orderinfo['receiver_phone'])?$orderinfo['receiver_mobile']:$orderinfo['receiver_mobile']." | ".$orderinfo['receiver_phone'] ;
								$log['address']=$orderinfo['receiver_state']." ".$orderinfo['receiver_city']." ".$orderinfo['receiver_district']." ".$orderinfo['town']." ".$orderinfo['address'] ;
								$log['goods_id']=$goodsinfo['id'];
								$log['outer_iid']=$orderinfo['outer_iid'] ;
								$log['num']=$orderinfo['num'] ;
								$log['amount']=$orderinfo['payment'] ;
								$log['remarks']='退款增加库存' ;
								$log['add_time']=date('Y-m-d H:i:s') ;
								$log['modify_time']=date('Y-m-d H:i:s') ;
								$log['tid']=$orderinfo['tid'] ;
								$log['oid']=$orderinfo['oid'] ;
								$log['refund_fee']=$refund['refund_fee'] ;
								$log['visitor_id']	   = $visitor_id;
								$rel=M('stock_log')->add($log);  /* 添加库存记录  */
							}
						}
						
					}
					
				}
				else
				{   
				    $refund['oid']=$oid;
					$res=$this->getOneTrade($tid,$appkey,$secretkey,$sessionkey,$refund,$visitor_id);
				}
				unset($refund);
				unset($orderinfo);
			    
		   }
		
		    
	    }
		else
		{
			if((string)$resp->total_results==0)
			{
				echo '暂无订单';
			}
			echo $resp->msg;
			$arr['error']=$resp->msg;
		}
	
		$arr['count']=$count;
		$arr['page_count']=$page_count;
		$arr['page']=$page;
		
		return $arr; 
   }
	
	
   /**
	 * 获取单条出售交易数据
	 * @param  [type] $tid        [description]
	 * @param  [type] $appkey     [description]
	 * @param  [type] $secret     [description]
	 * @param  [type] $sessionKey [description]
	 * @return [type]             [description]
	 */
	public function getOneTrade($tid,$appkey,$secret,$sessionKey,$refund='',$visitor_id){ 
		//$visitor_id = $_SESSION['user']['id']; 
		$trades = M("trades"); 	
		$orders = M("orders"); 	 
			
		$c = new \TopClient;
		$c->appkey = $appkey;
		$c->secretKey = $secret;
		$req = new \TradeFullinfoGetRequest;
		
		$field="seller_nick,pic_path,payment,seller_rate,post_fee,receiver_name,receiver_state,receiver_address,receiver_zip,receiver_mobile,receiver_phone,".
		       "consign_time,received_payment,invoice_kind,receiver_country,receiver_town,order_tax_fee,tid,num,num_iid,status,title,type,price,discount_fee,".
			   "has_post_fee,total_fee,created,pay_time,modified,end_time,buyer_message,buyer_memo,buyer_flag,seller_memo,seller_flag,invoice_name,invoice_type,".
			   "buyer_nick,credit_card_fee,mark_desc,shipping_type,buyer_cod_fee,adjust_fee,trade_from,buyer_rate,receiver_city,receiver_district,orders";
		$req->setFields($field);
		$req->setTid($tid);//订单tid
		$resp = $c->execute($req, $sessionKey);
           
		if(isset($resp->trade)){
			$trade = $resp->trade;
			
			$data['seller_nick'] 				=	(string)$trade -> seller_nick;
			$data['pic_path'] 				    =	(string)$trade -> pic_path;
			$data['payment'] 	                =	(string)$trade -> payment;
			$data['seller_rate'] 				=	(string)$trade -> seller_rate;
			$data['post_fee'] 					=	(string)$trade -> post_fee;
			$data['receiver_name'] 				=	(string)$trade -> receiver_name;
			$data['receiver_state'] 			=	(string)$trade -> receiver_state;
			$data['receiver_address'] 			=	(string)$trade -> receiver_address;
			$data['receiver_city'] 				=	(string)$trade -> receiver_city;
			$data['receiver_district'] 			=	(string)$trade -> receiver_district;
			$data['receiver_town'] 				=	(string)$trade -> receiver_town;
			$data['receiver_country'] 			=	(string)$trade -> receiver_country;
			$data['receiver_zip'] 				=	(string)$trade -> receiver_zip;
			$data['receiver_mobile'] 			=	(string)$trade -> receiver_mobile;
			$data['receiver_phone'] 			=	(string)$trade -> receiver_phone;
			$data['consign_time'] 				=	(string)$trade -> consign_time;
			$data['received_payment'] 			=	(string)$trade -> received_payment;
			$data['invoice_kind'] 				=	(string)$trade -> invoice_kind;
			$data['order_tax_fee'] 				=	(string)$trade -> order_tax_fee;
			$data['tid'] 			            =	(string)$trade -> tid;
			$data['num'] 		              	=	(string)$trade -> num;
			$data['num_iid'] 			        =	(string)$trade -> num_iid;
			$data['status'] 				    =	(string)$trade -> status;
			$data['title'] 			            =	(string)$trade -> title;
			$data['type'] 			            =	(string)$trade -> type;
			$data['price'] 			            =	(string)$trade -> price;
			$data['discount_fee'] 			    =	(string)$trade -> discount_fee; 
			$data['has_post_fee'] 				=	(string)$trade -> has_post_fee;
			$data['total_fee'] 			        =	(string)$trade -> total_fee;
			$data['receiver_zip'] 				=	(string)$trade -> receiver_zip;
			$data['seller_nick'] 				=	(string)$trade -> seller_nick;
			$data['seller_rate'] 				=	(string)$trade -> seller_rate;
			$data['shipping_type'] 				=	(string)$trade -> shipping_type;
			$data['sid'] 						=	(string)$trade -> sid;
			$data['status'] 					=	(string)$trade -> status;
			$data['title'] 						=	(string)$trade -> title;
			$data['total_fee'] 					=	(string)$trade -> total_fee;
			$data['created'] 					=	(string)$trade -> created;
			$data['pay_time'] 				    =	(string)$trade -> pay_time;
			$data['modified'] 				    =	(string)$trade -> modified;
			$data['end_time'] 				    =	(string)$trade -> end_time;
			$data['buyer_message'] 			    =	(string)$trade -> buyer_message;
			$data['buyer_memo'] 			    =	(string)$trade -> buyer_memo; 
			$data['buyer_flag'] 			    =	(string)$trade -> buyer_flag;
			$data['seller_memo'] 			    =	(string)$trade -> seller_memo; 
			$data['seller_flag'] 				=	(string)$trade -> seller_flag;
			$data['invoice_name'] 			    =	(string)$trade -> invoice_name;
			$data['invoice_type'] 				=	(string)$trade -> invoice_type;
			$data['buyer_nick'] 				=	(string)$trade -> buyer_nick;
			$data['credit_card_fee'] 			=	(string)$trade -> credit_card_fee;
			$data['mark_desc'] 				    =	(string)$trade -> mark_desc;
			$data['shipping_type'] 				=	(string)$trade -> shipping_type;
			$data['buyer_cod_fee'] 				=	(string)$trade -> buyer_cod_fee;
			$data['adjust_fee'] 				=	(string)$trade -> adjust_fee;
			$data['trade_from'] 				=	(string)$trade -> trade_from;
			$data['buyer_rate'] 				=	(string)$trade -> buyer_rate;
			$data['modified'] 				    =	(string)$trade -> modified;
			$data['sh_status'] 		            =	"0";
			$data['is_sh'] 		                =	1;
			$data['shifa'] 						=	(string)$trade -> num;
			$data['visitor_id']	  	 			= 	$visitor_id;
			$wheret['tid']=$data['tid'];
			//$wheret['visitor_id'] = $visitor_id; 
			$tradesinfo=$trades->field('id,tid')->where($wheret)->find();
			if($tradesinfo)
			{
				
			}
			else
			{
				$ret=$trades->add($data);
			}
			
				
			foreach($trade->orders->order as $vo)
			{  
			    $oid=(string)$vo->oid;
			    if($oid==$refund['oid'])
				{
					$data_order=$refund;
				}
				
				$data_order['adjust_fee']         = (string)$vo->adjust_fee;
				$data_order['buyer_rate']         = (string)$vo->buyer_rate;
				$data_order['cid']                = (string)$vo->cid;
				$data_order['discount_fee']       = (string)$vo->discount_fee;
				$data_order['end_time']           = (string)$vo->end_time;
				$data_order['is_daixiao']         = (string)$vo->is_daixiao;
				$data_order['num']                = (string)$vo->num;
				$data_order['num_iid']            = (string)$vo->num_iid;
				$data_order['oid']                = $oid;
				$data_order['tid']                = $data['tid'];
				$data_order['order_from']         = (string)$vo->order_from;
				$data_order['part_mjz_discount']  = (string)$vo->part_mjz_discount;
				$data_order['payment']            = (string)$vo->payment;
				$data_order['pic_path']           = (string)$vo->pic_path;
				$data_order['price']              = (string)$vo->price;
				//$data_order['refund_id']          = (string)$vo->refund_id;
				//$data_order['refund_status']      = (string)$vo->refund_status;
				$data_order['seller_rate']        = (string)$vo->seller_rate;
				$data_order['seller_type']        = (string)$vo->seller_type; 
				$data_order['status']             = (string)$vo->status;
				$data_order['title']              = (string)$vo->title;
				$data_order['total_fee']          = (string)$vo->total_fee;
				$data_order['outer_iid']          = (string)$vo->outer_iid;
				$data_order['visitor_id']	   	  = $visitor_id;
			
				$whereo['oid']=$oid;
				//$whereo['visitor_id']=$visitor_id;
				$orderinfo=$orders->field('id,oid,tid')->where($whereo)->find();
				
				   
				if($orderinfo)
				{
					$reo=$orders->where($whereo)->save($data_order);
				}
				else
				{
					$reo=$orders->add($data_order);
				}
				
				if($reo!==false)
				{
				    /* 退款成功且有退货的增加库存、库存记录 */
					if($refund['refund_status']=='SUCCESS' && $refund['has_good_return']=='true')
					{   
					    $shopinfo=M('shop')->field('id,shop_name')->where("visitor_id = '$visitor_id' and shop_name='".$data['title']."'")->find();
						
						$goodsinfo=M('goods')->field("id,code,stock")->where("visitor_id = '$visitor_id' and code='".$data_order['outer_iid']."'")->find(); 
						
						if($goodsinfo)
						{
						    $goods_stock['stock']=$goodsinfo['stock']+$orderinfo['num'] ;
							$where_info['id'] = $goodsinfo['id'];
							$where_info['visitor_id'] = $visitor_id;
							$reg=M('goods')->where($where_info)->save($goods_stock); /* 退款加库存  */
							
							if($reg)
							{ 
								$log['shop']=$shopinfo['id'] ;
								$log['name']=$data['receiver_name'] ;
								$log['phone']=empty($data['receiver_phone'])?$data['receiver_mobile']:$data['receiver_mobile']." | ".$data['receiver_phone'] ;
								$log['address']=$data['receiver_state']." ".$data['receiver_city']." ".$data['receiver_district']." ".$data['town']." ".$data['address'] ;
								$log['goods_id']=$goodsinfo['id'];
								$log['outer_iid']=$data_order['outer_iid'] ;
								$log['num']=$data_order['num'] ;
								$log['amount']=$data_order['payment'] ;
								$log['remarks']='退款增加库存' ;
								$log['add_time']=date('Y-m-d H:i:s') ;
								$log['modify_time']=date('Y-m-d H:i:s') ;
								$log['tid']=$data_order['tid'] ;
								$log['oid']=$data_order['oid'] ;
								$log['refund_fee']=$refund['refund_fee'] ; 
								$log['visitor_id']	   = $visitor_id;
								$rel=M('stock_log')->add($log);  /* 添加库存记录  */
							}	
						}
						
					}
				}
				
			}
		}
		
		return $resp;
	}
	
	
	
	
	
}
















